<%--
  Created by IntelliJ IDEA.
  User: admin
  Date: 2022/10/8
  Time: 13:50
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>Title</title>
    <script src="${pageContext.request.contextPath}/webjars/jquery/3.6.0/jquery.min.js"></script>
</head>
<body>
<form id="uploadForm" method="post" enctype="multipart/form-data">
    file: <input type="file" name="source"/> <br>
    <input type="text" name="username">
</form>
<button id="btn">提交</button>
<hr>
<a href="${pageContext.request.contextPath}/download/test1?name=Koala.jpg">下载</a>
</body>
<script>
    $("#btn").click(
        function () {
            var form = new FormData(document.getElementById("uploadForm"));
            $.ajax(
                {
                    url: "${pageContext.request.contextPath}/upload/test3",
                    type: "post",
                    data: form,
                    // 使用ajax请求去上传文件需要把如下两个属性设置为false，固定写法
                    processData:false,
                    contentType:false,
                    success: function (resp) {
                        console.log(resp);
                    },
                    error:function (resp) {
                        console.log(resp);
                    }
                }
            );
        }
    );
</script>
</html>
